using UnityEngine;
using UnityEngine.UI;

namespace MaterialUI
{
	[AddComponentMenu("MaterialUI/Vertical Scroll Layout Element", 50)]
	public class VerticalScrollLayoutElement : MonoBehaviour, ILayoutElement
	{
		[SerializeField]
		private float m_MaxHeight;

		[SerializeField]
		private RectTransform m_ContentRectTransform;

		[SerializeField]
		private ScrollRect m_ScrollRect;

		private RectTransform m_ScrollRectTransform;

		[SerializeField]
		private Image m_ScrollHandleImage;

		[SerializeField]
		private ScrollRect.MovementType m_MovementTypeWhenScrollable;

		[SerializeField]
		private Image[] m_ShowWhenScrollable;

		private bool m_ScrollEnabled;

		private float m_Height;

		public float maxHeight
		{
			get
			{
				return m_MaxHeight;
			}
			set
			{
				m_MaxHeight = value;
				RefreshLayout();
			}
		}

		public RectTransform contentRectTransform
		{
			get
			{
				return m_ContentRectTransform;
			}
			set
			{
				m_ContentRectTransform = value;
				RefreshLayout();
			}
		}

		public ScrollRect scrollRect
		{
			get
			{
				return m_ScrollRect;
			}
			set
			{
				m_ScrollRect = value;
				RefreshLayout();
			}
		}

		public RectTransform scrollRectTransform
		{
			get
			{
				return m_ScrollRectTransform;
			}
			set
			{
				m_ScrollRectTransform = value;
				RefreshLayout();
			}
		}

		public Image scrollHandleImage
		{
			get
			{
				return m_ScrollHandleImage;
			}
			set
			{
				m_ScrollHandleImage = value;
				RefreshLayout();
			}
		}

		public ScrollRect.MovementType movementTypeWhenScrollable
		{
			get
			{
				return m_MovementTypeWhenScrollable;
			}
			set
			{
				m_MovementTypeWhenScrollable = value;
				RefreshLayout();
			}
		}

		public bool scrollEnabled => m_ScrollEnabled;

		public float minWidth => -1f;

		public float preferredWidth => -1f;

		public float flexibleWidth => -1f;

		public float minHeight => m_Height;

		public float preferredHeight => m_Height;

		public float flexibleHeight => m_Height;

		public int layoutPriority => 0;

		private void RefreshLayout()
		{
			if (!m_ScrollRect)
			{
				m_ScrollRect = GetComponent<ScrollRect>();
			}
			if (!m_ScrollRectTransform)
			{
				m_ScrollRectTransform = m_ScrollRect.GetComponent<RectTransform>();
			}
			LayoutRebuilder.ForceRebuildLayoutImmediate(contentRectTransform);
			float preferredHeight = LayoutUtility.GetPreferredHeight(contentRectTransform);
			if (preferredHeight > m_MaxHeight)
			{
				m_Height = maxHeight;
				m_ScrollRect.movementType = movementTypeWhenScrollable;
				m_ScrollHandleImage.enabled = true;
				m_ScrollEnabled = true;
				for (int i = 0; i < m_ShowWhenScrollable.Length; i++)
				{
					m_ShowWhenScrollable[i].enabled = true;
				}
			}
			else
			{
				m_Height = preferredHeight;
				m_ScrollRect.movementType = ScrollRect.MovementType.Clamped;
				m_ScrollHandleImage.enabled = false;
				m_ScrollEnabled = false;
				for (int j = 0; j < m_ShowWhenScrollable.Length; j++)
				{
					m_ShowWhenScrollable[j].enabled = false;
				}
			}
			RectTransform scrollRectTransform = m_ScrollRectTransform;
			Vector2 sizeDelta = m_ScrollRectTransform.sizeDelta;
			scrollRectTransform.sizeDelta = new Vector2(sizeDelta.x, m_Height);
		}

		public void CalculateLayoutInputHorizontal()
		{
		}

		public void CalculateLayoutInputVertical()
		{
			RefreshLayout();
		}
	}
}
